﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using zhangmenren.IService;
using zhangmenren.Models;
using zhangmenren.Repository;

namespace zhangmenren.Service
{
	public class UserService : IUserService
	{
		private UserRepository userRepository = new UserRepository();

		/// <summary>
		/// 只是给注册用户使用，只添加了用户的手机号，用户名和密码
		/// </summary>
		/// <returns>The add.</returns>
		/// <param name="user">User.</param>
		public Task<int> Add(UserEntity user)
		{
			return Task.FromResult(userRepository.Add(user));
		}

		public Task<IEnumerable<UserEntity>> Selelct(int page = 1, int lim = 50, string key = "")
		{
			return Task.FromResult(userRepository.Select(page,lim,key));
		}

		public Task<IEnumerable<UserEntity>> Find()
		{
			return Task.FromResult(userRepository.Find());
		}


		public Task<UserEntity> Find(string username)
		{
			var list = userRepository.FindUserName(username);
			if (list.Any())
			{
				return Task.FromResult(list.FirstOrDefault());
			}
			else
			{
				return Task.FromResult(new UserEntity());
			}
		}

		public Task<UserEntity> FindMobile(string mobile)
		{
			var list = userRepository.FindMobile(mobile);
			if (list.Any())
			{
				return Task.FromResult(list.FirstOrDefault());
			}
			else
			{
				return Task.FromResult(new UserEntity());
			}
		}



		public Task<UserEntity> FindOpenId(string openId)
		{
			var list = userRepository.FindOpenId(openId);
			if (list.Any())
			{
				return Task.FromResult(list.FirstOrDefault());
			}
			else
			{
				return Task.FromResult(new UserEntity());
			}
		}

		public Task<UserEntity> FindqqId(string qqId)
		{
			var list = userRepository.FindOpenId(qqId);
			if (list.Any())
			{
				return Task.FromResult(list.FirstOrDefault());
			}
			else
			{
				return Task.FromResult(new UserEntity());
			}
		}

		public Task<UserEntity> FindById(int id)
		{
			var list = userRepository.FindByUserId(id);
			if (list.Any())
			{
				return Task.FromResult(list.FirstOrDefault());
			}
			else
			{
				return Task.FromResult(new UserEntity());
			}
		}


        public Task<int> SetZMRID(int zmrid, int id)
        {
            return Task.FromResult(userRepository.SetZMRID(zmrid, id));
        }


        #region 修改个人信息使用

        public Task<int> SetHead(string head, int id)
		{
			return Task.FromResult(userRepository.SetHead(head, id));
		}

		public Task<int> SetMobile(string mobile, int id)
		{
			return Task.FromResult(userRepository.SetMobile(mobile, id));
		}

		public Task<int> SetMaoShu(string miaoshu, int id)
		{
			return Task.FromResult(userRepository.SetMiaoShu(miaoshu, id));
		}

		public Task<int> SetUserName(string username, int id)
		{
			return Task.FromResult(userRepository.SetUserName(username, id));
		}

		public Task<int> SetAddress(string address, int id)
		{
			return Task.FromResult(userRepository.SetAddress(address, id));
		}
		public Task<int> SetPwd(string pwd, int id)
		{
			return Task.FromResult(userRepository.SetPwd(pwd, id));
		}

		public Task<int> SetPwd(string pwd, string mobile)
		{
			return Task.FromResult(userRepository.SetPwd(pwd, mobile));
		}

        public Task<int> SetTuiJian(int id,DateTime date)
        {
            return Task.FromResult(userRepository.SetIsTuiJian(id,date));
        }

        public Task<UserEntity> SelectTuiJia()
        {
            var list = userRepository.SelectTuiJian();
            if (list.Any())
            {
                return Task.FromResult(list.FirstOrDefault());
            }
            else
            {
                return Task.FromResult(new UserEntity());
            }
        }

        public Task<UserEntity> FindByZMRID(int zmrid)
        {
            return Task.FromResult(userRepository.FindByZMRID(zmrid).FirstOrDefault());
        }

        public Task<int> SetJianJie(string jianjie, int id)
        {
            return Task.FromResult(userRepository.SetJianJie(jianjie, id));
        }

        public Task<int> AddWeiXin(UserEntity user)
        {
            return Task.FromResult(userRepository.AddWeXin(user));

        }
        #endregion
    }
}
